package com.zhangtao.demo.common.enums;

import com.alibaba.fastjson.JSONObject;
import com.zhangtao.demo.common.dto.RedisKeyScanParam;
import com.zhangtao.demo.common.dto.WebSocketCommand;
import com.zhangtao.demo.common.util.GlobalComponentHolder;
import com.zhangtao.demo.common.vo.ResponseResult;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import redis.clients.jedis.ScanResult;

import java.util.HashMap;
import java.util.Map;

public enum WebSocketCommandEnum {
    SCAN {
        @Override
        public TextWebSocketFrame resolveCommand(WebSocketCommand command) {
            RedisKeyScanParam scanParam = JSONObject.parseObject(command.getValue(), RedisKeyScanParam.class);
            ScanResult<String> scanRes = GlobalComponentHolder.jedisController.scanTwo(scanParam.getCursor(), scanParam.getKeyPattern());
            return new TextWebSocketFrame(JSONObject.toJSONString(new ResponseResult(command, scanRes)));
        }
    },
    HOST_INFO {
        @Override
        public TextWebSocketFrame resolveCommand(WebSocketCommand command) {
            Map<String, JSONObject> hostInfo = GlobalComponentHolder.jedisController.info(null);
            return new TextWebSocketFrame(JSONObject.toJSONString(new ResponseResult(command, hostInfo)));
        }
    },
    KEYSPACE_INFO{
        @Override
        public TextWebSocketFrame resolveCommand(WebSocketCommand command) {
            Map<String, JSONObject> keyspaceInfo = GlobalComponentHolder.jedisController.getHostKeyspaceInfo();
            return new TextWebSocketFrame(JSONObject.toJSONString(new ResponseResult(command, keyspaceInfo)));
        }
    },
    HSCAN {
        @Override
        public TextWebSocketFrame resolveCommand(WebSocketCommand command) {
            return null;
        }
    };
    public abstract TextWebSocketFrame resolveCommand(WebSocketCommand command);
    public static final HashMap<String, WebSocketCommandEnum> commandMap = new HashMap<>(4);
    static {
        for (WebSocketCommandEnum value : WebSocketCommandEnum.values()) {
            commandMap.put(value.name(), value);
        }
    }
}
